package com.ys.filelib.filedialog;

import java.io.File;
import java.util.Collections;
import java.util.List;

public class FileSort {
    //名称 升序
    public static final int SORT_BY_NAME_ASC = 0;
    //名称 降序
    public static final int SORT_BY_NAME_DESC = 1;
    //日期 升序
    public static final int SORT_BY_DATE_ASC = 2;
    //日期 降序
    public static final int SORT_BY_DATE_DESC = 3;
    //大小 升序
    public static final int SORT_BY_SIZE_ASC = 4;
    //大小 降序
    public static final int SORT_BY_SIZE_DESC = 5;


    public static void sort(List<FileInfo> list, int sort) {
        Collections.sort(list, (o1, o2) -> compare(o1, o2, sort));
    }

    public static void sortBy(List<File> list, int sort) {
        Collections.sort(list, (o1, o2) -> compare(o1, o2, sort));
    }

    private static int compare(File o1, File o2, int sort) {
        if (o1.isDirectory() && !o2.isDirectory()) {
            return -1;
        }
        if (!o1.isDirectory() && o2.isDirectory()) {
            return 1;
        }
        switch (sort) {
            case SORT_BY_NAME_ASC:
            case SORT_BY_NAME_DESC:
                if (sort == SORT_BY_NAME_ASC) {
                    return o1.getName().compareToIgnoreCase(o2.getName());
                }
                return o2.getName().compareToIgnoreCase(o1.getName());
            case SORT_BY_DATE_ASC:
            case SORT_BY_DATE_DESC:
                long diff = o1.lastModified() - o2.lastModified();
                if (diff > 0) {
                    return (sort == SORT_BY_DATE_ASC ? 1 : -1);
                }
                if (diff == 0) {
                    return 0;
                }
                return (sort == SORT_BY_DATE_ASC ? -1 : 1);
            case SORT_BY_SIZE_ASC:
            case SORT_BY_SIZE_DESC:
                if (o1.isDirectory() && o2.isDirectory()) {
                    return o1.getName().compareToIgnoreCase(o2.getName());
                }
                long size = o1.length() - o2.length();
                if (size > 0) {
                    return (sort == SORT_BY_SIZE_ASC ? 1 : -1);
                }
                if (size == 0) {
                    return 0;
                }
                return (sort == SORT_BY_SIZE_ASC ? -1 : 1);
        }
        return 0;
    }

    private static int compare(FileInfo o1, FileInfo o2, int sort) {
        if (o1.isDirectory() && !o2.isDirectory()) {
            return -1;
        }
        if (!o1.isDirectory() && o2.isDirectory()) {
            return 1;
        }
        switch (sort) {
            case SORT_BY_NAME_ASC:
            case SORT_BY_NAME_DESC:
                if (sort == SORT_BY_NAME_ASC) {
                    return o1.getName().compareToIgnoreCase(o2.getName());
                }
                return o2.getName().compareToIgnoreCase(o1.getName());
            case SORT_BY_DATE_ASC:
            case SORT_BY_DATE_DESC:
                long diff = o1.getTime() - o2.getTime();
                if (diff > 0) {
                    return (sort == SORT_BY_DATE_ASC ? 1 : -1);
                }
                if (diff == 0) {
                    return 0;
                }
                return (sort == SORT_BY_DATE_ASC ? -1 : 1);
            case SORT_BY_SIZE_ASC:
            case SORT_BY_SIZE_DESC:
                if (o1.isDirectory() && o2.isDirectory()) {
                    return o1.getName().compareToIgnoreCase(o2.getName());
                }
                long size = o1.getSize() - o2.getSize();
                if (size > 0) {
                    return (sort == SORT_BY_SIZE_ASC ? 1 : -1);
                }
                if (size == 0) {
                    return 0;
                }
                return (sort == SORT_BY_SIZE_ASC ? -1 : 1);
        }
        return 0;
    }
}
